package com.dwxt.community.controller;

import com.dwxt.common.base.*;
import com.dwxt.common.util.*;
import com.dwxt.community.entity.*;
import com.dwxt.community.entity.dto.*;
import com.dwxt.community.service.FileService;
import org.apache.commons.lang3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.*;
import javax.validation.*;
import java.io.IOException;
import java.util.*;

@RequestMapping("/cms/file")
@RestController
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * showdoc
     *
     * @param file 必选 File 图片文件
     * @return {"status":200,"msg":"SUCCESS","data":"dsada"}
     * @catalog 社区/用户
     * @title 上传头像
     * @description 上传头像
     * @url /cms/file/uploadHeadPicture
     */
    @RequestMapping("/uploadHeadPicture")
    public BaseResult uploadHeadPicture(MultipartFile file) throws IOException {
        String userId = PartyUtil.getCurrentUserId();
        String imgUrl = fileService.saveFile(file, userId);
        return BaseResult.ok(imgUrl);
    }

    /**
     * showdoc
     * @catalog 社区/文件上传
     * @title 文件上传，保存在本地
     * @method  post
     * @param file 必须 MultipartFile 文件流
     * @description  文件上传
     * @url /cms/file/saveFileLocal
     * @return {"status":200,"msg":"SUCCESS","data":"文件路径"}
     */
    @RequestMapping("/saveFileLocal")
    public BaseResult saveFileLocal(MultipartFile file) throws IOException {
        String fileUrl = fileService.saveFile(file, null);
        return BaseResult.ok(fileUrl);
    }

    /**
     * showdoc
     * @catalog 社区/文件上传
     * @title 文件上传，保存在FastDFS服务器
     * @method  post
     * @param file 必须 MultipartFile 文件流
     * @description  文件上传
     * @url /cms/file/saveFile
     * @return {"status":200,"msg":"SUCCESS","data":"文件路径"}
     */
    @RequestMapping("/saveFile")
    public BaseResult saveFile(MultipartFile file) throws IOException {
        if(ObjectUtils.isEmpty(file)){
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        String fileUrl = fileService.saveFile(file);
        return BaseResult.ok(fileUrl);
    }

    /**
     * showdoc
     * @catalog 社区/文件上传
     * @title 多文件上传，保存在本地
     * @method  post
     * @param files 必须 List<MultipartFile> 文件流数组
     * @description  文件上传
     * @url /cms/file/saveFileList
     * @return {"status":200,"msg":"SUCCESS","data":"文件路径"}
     */
    @RequestMapping("/saveFileList")
    public BaseResult saveFileList(List<MultipartFile> files) throws IOException {
        StringBuilder sb = new StringBuilder();
        for (MultipartFile file : files) {
            String fileUrl = fileService.saveFile(file, null);
            sb.append(fileUrl).append(",");
        }
        return BaseResult.ok(sb.deleteCharAt(sb.length() - 1));
    }

    /**
     * showdoc
     * @catalog 社区/文件上传
     * @title 文件分片上传，保存在本地
     * @method  post
     * @param chunkUploadInfo 必须 ChunkUploadInfo 分片文件上传对象
     * @description  文件上传
     * @url /cms/file/chunkUploadLocal
     * @return {"status":200,"msg":"SUCCESS","data":"文件路径"}
     */
    @RequestMapping("/chunkUploadLocal")
    public BaseResult chunkUpload(@Valid ChunkUploadInfo chunkUploadInfo){
        if (chunkUploadInfo == null || StringUtils.isBlank(chunkUploadInfo.getId())) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        return fileService.chunkUpload(chunkUploadInfo);
    }

    /**
     * showdoc
     * @catalog 社区/文件上传
     * @title 获取分片信息
     * @method  post
     * @param task_id 必须 任务ID
     * @description  文件上传
     * @url /cms/file/chunkInfo
     * @return {"status":200,"msg":"SUCCESS","data":{"id":"158692026574007","chunk":null,"chunks":null,"fileName":null,"createTime":null,"dir":null,"offset":null,"fdfsFullPath":null,"size":null,"name":null,"fdfsPath":null,"fdfsGroup":null}}
     */
    @RequestMapping("/chunkInfo")
    public BaseResult chunkInfo(String task_id){
        if (StringUtils.isBlank(task_id)) {
            ChunkInfo chunkInfo = new ChunkInfo();
            chunkInfo.setId(IDUtils.genItemId());
            return BaseResult.ok(chunkInfo);
        }else {
            return fileService.getChunkInfo(task_id);
        }
    }

    /**
     * showdoc
     * @catalog 社区/文件上传
     * @title 文件分片上传，保存在FastDFS服务器
     * @method  post
     * @param file 必须 MultipartFile 文件流
     * @param id 必须 String 任务ID
     * @param chunk 必须 Integer 当前块
     * @param chunks 必须 Integer 总块数
     * @param fileName 必须 String 文件名
     * @param size 必须 Long 文件大小
     * @description  文件分片上传到FastDFS
     * @url /cms/file/chunkUpload
     * @return {"status":200,"msg":"SUCCESS","data":"http://125.62.47.85:18080/file/M03/00/01/wKgBf16We0uEHrBLAAAAAMY6E6Y686.mp4"}
     */
    @RequestMapping("/chunkUpload")
    public BaseResult chunkUploadToDFS(@Valid ChunkUploadInfo chunkUploadInfo) throws IOException {
        if (chunkUploadInfo == null || StringUtils.isBlank(chunkUploadInfo.getId())) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        return fileService.chunkUploadToDFS(chunkUploadInfo);
    }
}
